package com.esyoga.yoyoga.service.impl;

import com.esyoga.yoyoga.enums.BillTypeEn;
import com.esyoga.yoyoga.enums.UserTypeEn;
import com.esyoga.yoyoga.mapper.TContractMapper;
import com.esyoga.yoyoga.mapper.TOrderMapper;
import com.esyoga.yoyoga.pojo.TContract;
import com.esyoga.yoyoga.pojo.TOrder;
import com.esyoga.yoyoga.pojo.TOrderExample;
import com.esyoga.yoyoga.service.*;
import com.esyoga.yoyoga.util.OrderNoUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("orderService")
@Transactional
public class OrderServiceImpl implements OrderService {

    @Resource
    TOrderMapper tOrderMapper;
    @Resource
    TOrderExample tOrderExample;
    @Resource
    TContractMapper tContractMapper;
    @Resource
    StudentService studentService;
    @Resource
    TrainerService trainerService;
    @Resource
    GymService gymService;
    @Resource
    PayService payService;

    /**
     * 根据合约ID查找订单
     * @param cId
     * @return
     */
    @Override
    public List<TOrder> selectOrdersByContractId(Integer cId,  Integer oState) {
        tOrderExample.clear();
        TOrderExample.Criteria criteria = tOrderExample.createCriteria();
        criteria.andOCIdEqualTo(cId).andOStateEqualTo(oState);
        return tOrderMapper.selectByExample(tOrderExample);
    }

    /**
     * 创建订单
     * @param tOrder
     * @return
     */
    @Override
    public int insertOrder(TOrder tOrder) {
        return tOrderMapper.insert(tOrder);
    }

    /**
     * 更新订单
     * @param tOrder
     * @return
     */
    @Override
    public int updateOrder(TOrder tOrder) {
        return tOrderMapper.updateByPrimaryKeySelective(tOrder);
    }


    /**
     * 支付订单
     * @param oId
     * @return
     */
    @Override
    public Map<String,Object> payOrder(Integer oId) {

        Map<String,Object> result = new HashMap<>();
        TOrder tOrder = tOrderMapper.selectByPrimaryKey(oId);

        //1、查询所属合约
        TContract contract = tContractMapper.selectByPrimaryKey(tOrder.getoId());
        //2、学生支付,检查并更新学生余额
        BigDecimal orderMoney = tOrder.getoBanlance();//学生需要支付的金额
        String re = studentService.updateStudentBalance(orderMoney.negate(),contract.getcSId());//调用学生服务更新学生余额
        if(re.contains("余额不足")){
            result.put("result","fail");
            result.put("errorCode","001");
            result.put("reason","用户余额不足");
            return result;
        }
        payService.insertBillRecord(contract.getcSId(), UserTypeEn.STUDENT, BillTypeEn.PAY, OrderNoUtil.orderNoGenerate(),null);


        //3、更新教练余额
        BigDecimal trainerMoney = tOrder.getoGymbanlance();
        trainerService.updateTrainerBalance(trainerMoney,contract.getcTId());
        payService.insertBillRecord(contract.getcTId(), UserTypeEn.TRAINER,BillTypeEn.EARNINGS,OrderNoUtil.orderNoGenerate(),null);

        //4、更新场馆余额
        BigDecimal gymMoney = tOrder.getoTrainerbanlance();
        gymService.updateTGymBalance(gymMoney,contract.getcGId());
        payService.insertBillRecord(contract.getcGId(), UserTypeEn.GYM,BillTypeEn.EARNINGS,OrderNoUtil.orderNoGenerate(),null);

        //5、更新订单状态
        tOrder.setoState(1);    //将订单状态设置为已完成
        tOrderMapper.updateByPrimaryKeySelective(tOrder);
        result.put("result","success");
        return result;
    }
}
